<?php include("admin-header.php"); 
require_once 'check_tree.php';

foreach($_POST as $key => $value) {
 logWrite($key . " ---- ". $value);
            }
?>

<form name="submitAll" id="submitAll" action="edit2.php" method="post">
    <?php
    $headQuestionId = -10;
    //logic for inserting
   
   
    if($_POST['firstLink'] != "") {
        $nestingLevelCurrently = (int)$_POST['nestingLevel'];
        $ids=DAOFactory::getQuestionmasterDAO()->queryByNestingLevelAndFirstLink(($nestingLevelCurrently-1),$_POST['firstLink']);
    }


    if($_POST['questionMasterId'] != "") {
        $questionMasterId = $_POST['questionMasterId'];
        $questionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMasterId);
        $questionMasterId  = $questionMaster->firstLink;
        $firstLink = $questionMaster->firstLink;
    }
    if($_POST['previous'] != "true") {
        if($_POST['nestingLevel'] > 2) {

            $nestingLevelCurrently= $_POST['nestingLevel'];
            for($i=0;$i<count($ids);$i++) {

               // logWrite("for loop " . $i);
                $questionMasterId = $ids[$i]->questionMasterId;
                //logWrite("for loop this the qmi is " . $questionMasterId);

                //<----  load head
                $questionMaster = $ids[$i];

                //---------> load head
                //
                //
                //<---- yes box insert start

                $loaded = false;
                if($questionMaster->yesQuestionId == -1) {
                    //echo "b4 continue";
                    continue;
                    //echo "after continue";
                }else if($questionMaster->yesQuestionId != -10) {
                        $yesQuestionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMaster->yesQuestionId);
                        $loaded = true;
                }else {
                        $yesQuestionMaster = new Questionmaster();
                }



                $yesQuestionMaster->categoryMasterId = $questionMaster->categoryMasterId;
                $yesQuestionMaster->question = $_POST['yesQuestion' . $questionMasterId];
                if($_POST["yesQuestioncChkbox" . $questionMasterId] == "on") {
                    $yesQuestionMaster->yesQuestionId = -1;
                    $yesQuestionMaster->noQuestionId = -1;
                    $yesQuestionMaster->questionType = 2;
                }else if(!$loaded) {
                        $yesQuestionMaster->yesQuestionId = -10;
                        $yesQuestionMaster->noQuestionId = -10;
                        $yesQuestionMaster->questionType = 1;
                    }else if($loaded) {
                	if($yesQuestionMaster->noQuestionId == -1){
                		$yesQuestionMaster->yesQuestionId = -10;
                    	$yesQuestionMaster->noQuestionId = -10;
                	}
                	$yesQuestionMaster->questionType = 1;
                }
                $yesQuestionMaster->diagnosis = $_POST["yesDiagnosis" . $questionMasterId] . "";
                $yesQuestionMaster->isTreeFinalised=0;
                $yesQuestionMaster->nestingLevel = $nestingLevelCurrently;
                $yesQuestionMaster->parentQuestionId = $questionMasterId;
                $yesQuestionMaster->firstLink = $questionMaster->firstLink;
                $yesQuestionMaster->markForDelete = 0;
                if($_POST["yesQuestionUrl" . $questionMasterId] . "" == ""){
                	$yesQuestionMaster->url = $questionMaster->url;
                }else{
                	$yesQuestionMaster->url = $_POST["yesQuestionUrl" . $questionMasterId] . "";;
                }

                if(!$loaded) {
                    $yesId = DAOFactory::getQuestionmasterDAO()->insert($yesQuestionMaster);
                }else {
                    DAOFactory::getQuestionmasterDAO()->update($yesQuestionMaster);
                    $yesId = $yesQuestionMaster->questionMasterId;
                }



                //-----> yes box insert end
                $firstLink = $questionMaster->firstLink;
                // <---- no box insert start
                $loadedNo = false;
                if($questionMaster->noQuestionId == -1) {
                    //echo "b4 continue";
                    continue;
                    //echo "after continue";
                }else if($questionMaster->noQuestionId!=-10) {
                        $noQuestionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMaster->noQuestionId);
                        $loadedNo = true;
                    }else {
                        $noQuestionMaster = new Questionmaster();
                    }


                $noQuestionMaster->categoryMasterId = $questionMaster->categoryMasterId;
                $noQuestionMaster->question = $_POST['noQuestion' . $questionMasterId];
                if($_POST["noQuestioncChkbox" . $questionMasterId] == "on") {
                    $noQuestionMaster->yesQuestionId = -1;
                    $noQuestionMaster->noQuestionId = -1;
                    $noQuestionMaster->questionType = 2;
                }else if(!$loadedNo) {
                        $noQuestionMaster->yesQuestionId = -10;
                        $noQuestionMaster->noQuestionId = -10;
                        $noQuestionMaster->questionType = 1;
                    }else if($loadedNo) {
                	if($noQuestionMaster->noQuestionId == -1){
                		$noQuestionMaster->yesQuestionId = -10;
                    	$noQuestionMaster->noQuestionId = -10;
                	}
                	$noQuestionMaster->questionType = 1;
                }
                $noQuestionMaster->diagnosis = $_POST["noDiagnosis" . $questionMasterId] . "";
                $noQuestionMaster->isTreeFinalised=0;
                $noQuestionMaster->nestingLevel = $nestingLevelCurrently;
                $noQuestionMaster->parentQuestionId = $questionMasterId;
                $noQuestionMaster->firstLink = $questionMaster->firstLink;
                $noQuestionMaster->markForDelete = 0;
               
             if($_POST["noQuestionUrl" . $questionMasterId] . "" == ""){
                	$noQuestionMaster->url = $questionMaster->url;
                }else{
                	$noQuestionMaster->url = $_POST["noQuestionUrl" . $questionMasterId] . "";;
                }

                if(!$loadedNo) {
                    $noId = DAOFactory::getQuestionmasterDAO()->insert($noQuestionMaster);
                }else {
                    DAOFactory::getQuestionmasterDAO()->update($noQuestionMaster);
                    $noId = $noQuestionMaster->questionMasterId;
                }

                // -----> no box insert end

                // -----> update parent start

                $questionMaster->yesQuestionId =$yesId;
                $questionMaster->noQuestionId =$noId;
                $questionMaster->questionType=1;
                DAOFactory::getQuestionmasterDAO()->update($questionMaster);

                //logWrite("updates done  " . $i);

            //      for end  ----------------------------------------------------------------------
            }

        }else {

            //foreach($_POST as $key => $value) {
               // logWrite($key . " ---- ". $value);
            //}
            if($_POST['questionMasterId'] == "") {
            //<----------question title start
                $questionMasterTitle = new Questionmaster();
                $details = explode("@@@@", $_POST['category']);
                $questionMasterTitle->categoryMasterId = $details[0];
                $questionMasterTitle->question = $_POST['questionTitle'];
                $questionMasterTitle->noQuestionId = 0;
                $questionMasterTitle->yesQuestionId = 0;
                $questionMasterTitle->diagnosis="";
                $questionMasterTitle->isTreeFinalised=0;
                $questionMasterTitle->nestingLevel = 0;
                $questionMasterTitle->parentQuestionId = 0;
                $questionMasterTitle->firstLink = -10;
                $questionMasterTitle->markForDelete = 0;
                $questionMasterTitle->questionType = -1;
                $questionMasterTitle->url = $_POST['url'];
                $titleId = DAOFactory::getQuestionmasterDAO()->insert($questionMasterTitle);
                //----------->question title end

                //<----------question first start
                $questionMasterFirst = new Questionmaster();
                $details = explode("@@@@", $_POST['category']);
                $questionMasterFirst->categoryMasterId = $details[0];
                $questionMasterFirst->question = $_POST['initialQuestion'];
                $questionMasterFirst->diagnosis="";
                $questionMasterFirst->isTreeFinalised=0;
                $questionMasterFirst->nestingLevel = 1;
                $questionMasterFirst->parentQuestionId = $titleId;
                $questionMasterFirst->url = $_POST['url'];
                $questionMasterFirst->firstLink = -10;
                $questionMasterFirst->markForDelete = 0;
                $questionMasterFirst->questionType = 0;
                $questionMasterFirst->noQuestionId = -10;
                $questionMasterFirst->yesQuestionId = -10;
                $firstQuestionId = DAOFactory::getQuestionmasterDAO()->insert($questionMasterFirst);



                //----------->question first end
                //updates
                //echo $firstQuestionId .  " - " . $titleId;

                $questionMasterFirst = DAOFactory::getQuestionmasterDAO()->load($firstQuestionId);
                $questionMasterTitle = DAOFactory::getQuestionmasterDAO()->load($titleId);
                $questionMasterFirst->firstLink = $firstQuestionId;
                $questionMasterTitle->firstLink = $firstQuestionId;
                DAOFactory::getQuestionmasterDAO()->update($questionMasterFirst);
                DAOFactory::getQuestionmasterDAO()->update($questionMasterTitle);


                //end updates

                $questionMasterId = $firstQuestionId;
                $firstLink = $firstQuestionId;
            }

            //<---- yes box insert start

            $questionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMasterId);
$laad=false;
            if($questionMaster->yesQuestionId!=-10) {
$laad=true;
                $yesQuestionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMaster->yesQuestionId);
            }else {
            	$laad=false;
                $yesQuestionMaster = new Questionmaster();
            }


            $details = explode("@@@@", $_POST['category']);
            $yesQuestionMaster->categoryMasterId = $details[0];
            $yesQuestionMaster->question = $_POST['yesQuestion'];
            //echo "out of yes " . $_POST["yesQuestioncChkbox"] . "----" . $laad . "<br>";;
            if($_POST["yesQuestioncChkbox"] == "on") {
            	//echo "out of yes if " . $_POST["yesQuestioncChkbox"] . "----" . $laad . "<br>";;
                $yesQuestionMaster->yesQuestionId = -1;
                $yesQuestionMaster->noQuestionId = -1;
                $yesQuestionMaster->questionType = 2;
            }else if(!$laad) {
            	//echo "in of else " . $_POST["yesQuestioncChkbox"] . "----" . $laad . "<br>";;
                    $yesQuestionMaster->yesQuestionId = -10;
                    $yesQuestionMaster->noQuestionId = -10;
                    $yesQuestionMaster->questionType = 1;
                }else if($laad) {
                	if($yesQuestionMaster->noQuestionId == -1){
                		$yesQuestionMaster->yesQuestionId = -10;
                    	$yesQuestionMaster->noQuestionId = -10;
                	}
                	$yesQuestionMaster->questionType = 1;
                }
            $yesQuestionMaster->diagnosis = $_POST["yesDiagnosis"] . "";
            $yesQuestionMaster->isTreeFinalised=0;
            $yesQuestionMaster->nestingLevel = 2;
            $yesQuestionMaster->parentQuestionId = $questionMasterId;
            $yesQuestionMaster->firstLink = $questionMaster->firstLink;
            $yesQuestionMaster->markForDelete = 0;
          
          if($_POST["yesQuestionUrl"] . "" == ""){
                	$yesQuestionMaster->url = $questionMaster->url;
                }else{
                	$yesQuestionMaster->url = $_POST["yesQuestionUrl"] . "";;
                }
            if($_POST['questionMasterId'] == "") {
                $yesId = DAOFactory::getQuestionmasterDAO()->insert($yesQuestionMaster);
            }else {
                DAOFactory::getQuestionmasterDAO()->update($yesQuestionMaster);
                $yesId = $yesQuestionMaster->questionMasterId;
            }
            //-----> yes box insert end

            // <---- no box insert start
            $laad1 = false;
            if($questionMaster->noQuestionId!=-10) {
            	$laad1 = true;
                $noQuestionMaster = DAOFactory::getQuestionmasterDAO()->load($questionMaster->noQuestionId);
            }else {
            	$laad1 = false;
                $noQuestionMaster = new Questionmaster();
            }


            $details = explode("@@@@", $_POST['category']);
            $noQuestionMaster->categoryMasterId = $details[0];
            $noQuestionMaster->question = $_POST['noQuestion'];
           // echo "out of no " . $_POST["yesQuestioncChkbox"] . "----" . $laad . "<br>";
            if($_POST["noQuestioncChkbox"] == "on") {
            	//echo "in of if no " . $_POST["noQuestioncChkbox"] . "----" . $laad1;
                $noQuestionMaster->yesQuestionId = -1;
                $noQuestionMaster->noQuestionId = -1;
                $noQuestionMaster->questionType = 2;
            }else if(!$laad){
            	//echo "in of else no " . $_POST["noQuestioncChkbox"] . "----" . $laad1;
                    $noQuestionMaster->yesQuestionId = -10;
                    $noQuestionMaster->noQuestionId = -10;
                    $noQuestionMaster->questionType = 1;
                }else if($laad) {
                	if($noQuestionMaster->noQuestionId == -1){
                		$noQuestionMaster->yesQuestionId = -10;
                    	$noQuestionMaster->noQuestionId = -10;
                	}
                	$noQuestionMaster->questionType = 1;
                }
            $noQuestionMaster->diagnosis = $_POST["noDiagnosis"] . "";
            $noQuestionMaster->isTreeFinalised=0;
            $noQuestionMaster->nestingLevel = 2;
            $noQuestionMaster->parentQuestionId = $questionMasterId;
            $noQuestionMaster->firstLink = $questionMaster->firstLink;
            $noQuestionMaster->markForDelete = 0;
            

           if($_POST["noQuestionUrl"] . "" == ""){
                	$noQuestionMaster->url = $questionMaster->url;
                }else{
                	$noQuestionMaster->url = $_POST["noQuestionUrl"] . "";;
                }

            if($_POST['questionMasterId'] == "") {
                $noId = DAOFactory::getQuestionmasterDAO()->insert($noQuestionMaster);
            }else {
                DAOFactory::getQuestionmasterDAO()->update($noQuestionMaster);
                $noId = $noQuestionMaster->questionMasterId;
            }


            // -----> no box insert end

            // -----> update parent start

            $questionMaster->yesQuestionId =$yesId;
            $questionMaster->noQuestionId =$noId;
            $questionMaster->url =$_POST['url'];
            if($questionMaster->questionMasterId ==$questionMaster->firstLink){
                $questionMaster->questionType =0;
            }else{
                    $questionMaster->questionType =1;
            }
            DAOFactory::getQuestionmasterDAO()->update($questionMaster);
        // -----> update parent end
        //  exit("inserts done successfully.");
        }

    }
    ?> <?php

    $masterQuestionId =  $questionMasterId;
    $currentNestingLevel = $_POST['nestingLevel'];
    if($firstLink == null) {
        $firstLink = $_POST['firstLink'];
    }
    $arr = DAOFactory::getQuestionmasterDAO()->queryByNestingLevelAndFirstLink(($currentNestingLevel-1),$firstLink);
	// tree finalise check
		if(checktree($firstLink)){
			DAOFactory::getQuestionmasterDAO()->updateIsTreeFinalised($firstLink,true);
		}	else{
			DAOFactory::getQuestionmasterDAO()->updateIsTreeFinalised($firstLink,false);
		}
    //
	// delete all orphans
    	deleteAllOrphans($firstLink);
    //
    $headQuestionId = DAOFactory::getQuestionmasterDAO()->load($arr[0]->firstLink)->parentQuestionId;
    $questionIds = array();
    $elet = 0;

    for($i=0;$i<count($arr);$i++) {
        $row = $arr[$i];

        if(DAOFactory::getQuestionmasterDAO()->load($row->yesQuestionId)->yesQuestionId != -1 && $row->yesQuestionId != -1) {
            $questionIds[$elet++] = $row->yesQuestionId;
           // echo "qmi incre in yes for " . $row->yesQuestionId;
        }
        if(DAOFactory::getQuestionmasterDAO()->load($row->noQuestionId)->yesQuestionId != -1  && $row->noQuestionId != -1) {
            $questionIds[$elet++] = $row->noQuestionId;
           // echo "qmi incre in no for " . $row->noQuestionId;
        }
    }
   if($elet == 0){?>

<table width="380" cellpadding="15" bgcolor="#CCCCCC" >

	<tr>
							  <td colspan="2"><div align="center" class="styleLabel style15 style11  style17">Success </div></td>
							</tr>
							<tr>
							  <td class="style16" style="font-size:15px" colspan="2">

								Your changes have been saved successfully

                                                        		  <br><a href="<?php echo $base; ?>/php/create.php"><br>Click here</a> to go back to create another question.
		  </td>
                                                                                 </tr>

      </table>
                                                                          <?php
                                                                          exit("");
                                                                         
   }
    $allIds = "";
    foreach($questionIds as $key => $value) {

        $questionObj = DAOFactory::getQuestionmasterDAO()->load($value);
       // echo "anil was here and is here " . $elet;
        ?>


    <div id="set" style="width: 800px">
<div id="create2Error<?php echo $questionObj->questionMasterId; ?>" name="create2Error<?php echo $questionObj->questionMasterId; ?>" style="width: 800px" class="style21"></div>
            <?php
            echo "<br><div align='left'>&nbsp;&nbsp;&nbsp;&nbsp; <strong >Q: </strong><b>" . $questionObj->question . "</b></div><br>"; 
           // echo "<br><b>" . $questionObj->questionMasterId . "  " . $questionObj->yesQuestionId . "  " . $questionObj->noQuestionId . "<br><b>";
            $allIds = $allIds . "-" . $questionObj->questionMasterId;
            ?>

        <TABLE cellspacing="0">
<?php if($questionObj->yesQuestionId != null) {
                                                       $yesQuestionObj = DAOFactory::getQuestionmasterDAO()->load($questionObj->yesQuestionId);
                                                       $questionForPrint =  $yesQuestionObj->question;
                                                       
                                                     
                                                   }?>
            <TR>
                <TD VALIGN="TOP" class=questionbox >Yes Question :</TD>
                <TD VALIGN="TOP" class=questionbox><textarea rows="1" cols="30" disabled 
                                           name="yesQuestion<?php echo $questionObj->questionMasterId; ?>"
                                           id="yesQuestion<?php echo $questionObj->questionMasterId; ?>" <?php if($yesQuestionObj->questionType == 2) { echo "disabled"; } ?>><?php

                                                  echo $questionForPrint;

                            ?></textarea>
                </TD>
                <TD VALIGN="TOP" class=questionbox><font size=2px> <input type="checkbox" disabled 
                                                        name="yesQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>"
                                                        id="yesQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>"
                                                            <?php if($yesQuestionObj->questionType == 2) { echo "checked = 'checked'"; } ?>
                                                        onclick="show('yesQuestion<?php echo $questionObj->questionMasterId; ?>' ,'yesQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>','yesDiagnosis<?php echo $questionObj->questionMasterId; ?>')" />&nbsp;<br> Final Answer </font></TD>
                                                        <TD VALIGN="TOP" class=questionbox><textarea disabled  rows="1" cols="30" <?php if($yesQuestionObj->questionType != 2) { echo "disabled"; } ?>
                                           name="yesDiagnosis<?php echo $questionObj->questionMasterId; ?>"
                                           id="yesDiagnosis<?php echo $questionObj->questionMasterId; ?>"><?php if($yesQuestionObj->questionType == 2) { echo $yesQuestionObj->diagnosis; } ?></textarea>
               </TD>
            </TR>
                  <TR <?php if(!$urlConfiguration){?>style="display:  none; " <?php }?>>
                                    <TD VALIGN="TOP" class=urlbox><font size=2px>
								Yes Question's Url : &nbsp;</font>
                                    </TD>
                                    <TD VALIGN="TOP" class="urlbox" colspan="3">
                                        <input  disabled type="text"  name="yesQuestionUrl<?php echo $questionObj->questionMasterId; ?>" <?php if($no == true){ echo "disabled"; }  ?>  id="yesQuestionUrl<?php echo $questionObj->questionMasterId; ?>"  value="<?php echo $yesQuestionObj->url ?>" 	/>		<br>
                                    </TD>

                                   
									
                  </TR>
        </TABLE>





        <TABLE cellspacing="0">
            <TR>
            <?php  if($questionObj->noQuestionId != null) {
                                                       $noQuestionObj = DAOFactory::getQuestionmasterDAO()->load($questionObj->noQuestionId);
                                                      $questionForPrintNo =  $noQuestionObj->question;
                                                      
                                                   }?>
                <TD VALIGN="TOP" class=questionbox>No Question : &nbsp;</TD>
                <TD VALIGN="TOP" class=questionbox><textarea rows="1" disabled  cols="30" <?php if($noQuestionObj->questionType == 2) { echo "disabled"; } ?>
                                           name="noQuestion<?php echo $questionObj->questionMasterId; ?>"
                                           id="noQuestion<?php echo $questionObj->questionMasterId; ?>"><?php

                                                  echo $questionForPrintNo;

                            ?></textarea>
                </TD>

                <TD VALIGN="TOP" class=questionbox><font size=2px> <input type="checkbox" disabled 
                                                        name="noQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>"
                                                        id="noQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>"
                                                            <?php if($noQuestionObj->questionType == 2) { echo "checked = 'checked'"; } ?>
                                                        onClick="show('noQuestion<?php echo $questionObj->questionMasterId; ?>' ,'noQuestioncChkbox<?php echo $questionObj->questionMasterId; ?>', 'noDiagnosis<?php echo $questionObj->questionMasterId; ?>');">&nbsp; <br>Final Answer<br>
                       
                    </font></TD>
                    <TD VALIGN="TOP" class=questionbox><textarea disabled  rows="1" cols="30" <?php if($noQuestionObj->questionType != 2) { echo "disabled"; } ?>
                                           name="noDiagnosis<?php echo $questionObj->questionMasterId; ?>"
                                           id="noDiagnosis<?php echo $questionObj->questionMasterId; ?>"><?php if($noQuestionObj->questionType == 2) { echo $noQuestionObj->diagnosis; } ?></textarea></TD>


            </TR>
            <TR <?php if(!$urlConfiguration){?>style="display:  none; " <?php }?>>
                                    <TD VALIGN="TOP" class=urlbox><font size=2px>
								No Question's Url : &nbsp;</font>
                                    </TD>
                                    <TD VALIGN="TOP" class="urlbox" colspan="3">
                                        <input   disabled type="text"  name="noQuestionUrl<?php echo $questionObj->questionMasterId; ?>" <?php if($no == true){ echo "disabled"; }  ?>  id="noQuestionUrl<?php echo $questionObj->questionMasterId; ?>"  value="<?php echo $noQuestionObj->url ?>" 	/>		
                                    </TD>
			</TR>
<TR>
			              <TD colspan="3" VALIGN="TOP" class=questionbox>&nbsp;</TD>
			              <TD VALIGN="TOP" class=questionbox><input type="button" onclick="allowEditAll(<?php echo $questionObj->questionMasterId; ?>)" value=Edit style="color:white;font-weight:bold;float:right;background-color:#1A6A96;width:80px;height:25px"/></TD>
			 	</TR>


        </TABLE>


    </div>


    <?php
echo "<br>";
    }





    ?>
    <input type="hidden" name="allIds" id="allIds" value="<?php echo $allIds;?>" />

    <input name="nestingLevel" type="hidden" id="nestingLevel" value="<?php echo ($_POST['nestingLevel']+1)?>" />
    <input name="firstLink" type="hidden" id="firstLink" value="<?php echo $firstLink ?>" />
    <input name="questionId" type="hidden" id="questionId" value="<?php echo $headQuestionId; ?>" />
    <input name="previous" type="hidden" id="previous" value="" onclick="allowEditAll(<?php echo $questionObj->questionMasterId; ?>)"/>
    <input type="submit" id="submit" value="previous" onclick="previousPage(2);" style="color:white;font-weight:bold;background-color:#1A6A96;width:80px;height:25px" />
    <input type="submit" id="submit" value="next" onclick="allowEditPage();return validateCreate2();" style="color:white;font-weight:bold;background-color:#1A6A96;width:80px;height:25px"/>
    
</form>
<?php include("admin-footer.php"); ?>
